/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#ifndef __DIALECT_GLOBALS__H___
#define __DIALECT_GLOBALS__H___

#include "stdafx.h"	// determines whether we are using MFC
#if defined(__AFX_H__) && !defined(USING_MFC)
#	define USING_MFC
#endif

// Choose the target platform by uncommenting one of the following
// Or define one in the predefined compiler defines
//#define __WINNT__		// Also used for Win95/98
//#define __WINCE__
//#define __UNIX__

// This code automatically chooses __WINCE__ or __WINNT__ using
// some standard predefined symbols, if the OS isn't set already

#if !defined(__WINNT__) && !defined(__WINCE__) && !defined(__UNIX__)
#	ifdef UNDER_CE
#		define __WINCE__
#	else
#		ifdef WIN32
#			define __WINNT__
#		endif
#	endif
#endif

// If we still haven't found the OS, complain loudly & quit

#if !defined(__WINNT__) && !defined(__WINCE__) && !defined(__UNIX__)
#	error Operating system not specified!
#endif

//=============================================================
// If compiling to or linking as a DLL, define linkage as needed
//#define D_EXPORT	__declspec( dllexport ) //AFX_CLASS_EXT
//#define D_EXPORT	AFX_CLASS_EXT
#define D_EXPORT

//#ifdef _AFXDLL
//#	ifdef _AFXEXT
//#		define D_EXPORT __declspec( dllexport )
//#		pragma message("AFX_EXT_FUNC = DLL Export")
//#	else
//#		define D_EXPORT __declspec( dllimport)
//#		pragma message("AFX_EXT_FUNC = DLL Import")
//#	endif
//#endif

//=============================================================

#if defined(DIALECT_PRO) || defined(DIALECT_ENT)
#	define ADO_SUPPORT
#	define COMPORT_SUPPORT
#	define CRYPTO_SUPPORT
#	define DLL_SUPPORT // Ability to access DLLs from Dialect code (in system module)
#endif
#define MATH_MODULE_SUPPORT
#define IDE_SUPPORT

//=============================================================

#ifdef __WINNT__
#	define	__OPERATING_SYS__	_T("WINNT")
#	define	DIR_SEPARATOR	'\\'
#endif //_WINNT

#ifdef __WINCE__
#	define	__OPERATING_SYS__	_T("WINCE")
#	define	DIR_SEPARATOR	'\\'
#	ifndef UNICODE
#		define UNICODE
#	endif
#	ifndef UNDER_CE
#		define UNDER_CE
#	endif
#endif //_WINCE

#ifdef __UNIX__
#	define	__OPERATING_SYS__	_T("UNIX")
#	define	DIR_SEPARATOR	'/'
#endif //_UNIX

// ASCII/Unicode stdlib macros (includes TCHAR.h under Windows)
#include "DTCHAR.h"

//=============================================================

#if defined(__WINNT__) || defined(__WINCE__)
#	ifdef USING_MFC
#		include <afxwin.h>
#	else
#		include <windows.h>
#	endif
#endif

//=============================================================
#ifdef USING_MFC
// If MFC is being linked in, we can use CStrings instead of TStrings
#define TString CString	// Define this to use CStrings
#endif // USING_MFC

#ifdef TString
#pragma message("Using CString")
#else
#pragma message("Using TString")
#include "TString.h"	// Using CString here if MFC
#endif

//=============================================================
// Define the symbol __THREADS__ to enable multi-threading in Dialect
// (also makes the VALUE garbage collector thread-safe)
//#define __THREADS__	// Thread support is not ready yet

#ifdef __THREADS__
#	pragma message("Thread support is a on...")
#	if defined(__WINNT__) || defined(__WINCE__)
#		define MUTEX_T				HANDLE
#		define MUTEX_INIT(mutex)	mutex = CreateMutex(NULL, FALSE, NULL)
#		define MUTEX_DESTROY(mutex)	{CloseHandle(mutex); mutex = NULL;}
#		define MUTEX_LOCK(mutex)	WaitForSingleObject(mutex, INFINITE);
#		define MUTEX_UNLOCK(mutex)	ReleaseMutex(mutex)
#	else // __UNIX__	(using Posix threads)
#		include <pthread.h>
#		define MUTEX_T				pthread_mutex_t
#		define MUTEX_INIT(mutex)	pthread_mutex_init(&mutex, NULL)
#		define MUTEX_DESTROY(mutex)	;
#		define MUTEX_LOCK(mutex)	pthread_mutex_lock(&mutex)
#		define MUTEX_UNLOCK(mutex)	pthread_mutex_unlock(&mutex)
#	endif
#else // !__THREADS__
#		define MUTEX_T				void
#		define MUTEX_INIT(mutex)	;
#		define MUTEX_DESTROY(mutex)	;
#		define MUTEX_LOCK(mutex)	;
#		define MUTEX_UNLOCK(mutex)	;
#endif // __THREADS__

/*
 *
	{
	MUTEX_T m_GCMutex;
	MUTEX_INIT(m_GCMutex);

	MUTEX_LOCK(m_GCMutex);
	GARBAGE_COLLECT;
	MUTEX_UNLOCK(m_GCMutex);

	MUTEX_DESTROY(m_GCMutex);
	}
 *
 */

//=============================================================
#ifndef U8
#define U8		unsigned char
#endif
#ifndef U16
#define U16		unsigned short
#endif
#ifndef U32
#define U32		unsigned long
#endif
#ifndef DWORD
#define DWORD	unsigned long
#endif

//=============================================================

#define CORE_FUNC(x) \
	static VALUE x(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)

//=============================================================
#include "CDebug.h"
#include "CTokenTypes.h"
//=============================================================

// This is version number used in compiled and serialized files
// Compiled byte code files begin with {'d', 'c', FILE_VERSION_BYTE}
// Serialized data files begin with {'d', 's', FILE_VERSION_BYTE}
#define FILE_VERSION_BYTE	(U8)2

//=============================================================

#define FIRST_PASS_BY_REF	32

enum SymbolTypes {
	stUndefined = 0,
	stNil = 1,
	stString = 2,
	stFloat = 3,
	stInteger = 4,
	stBoolean = 5,
	// Put all pass by reference types below stArray
	// A pass by reference type uses VALUE::ci to hold
	// a pointer to a CImplementer derived class and is
	// garbage collected within the VALUE class
	stArray = FIRST_PASS_BY_REF,
	stFrame	= FIRST_PASS_BY_REF + 1,
	stClass = FIRST_PASS_BY_REF + 2,
	stClassInstance = FIRST_PASS_BY_REF + 3,
	stFunction = FIRST_PASS_BY_REF + 4,
	stSymbol = FIRST_PASS_BY_REF + 5,
	stProperty = FIRST_PASS_BY_REF + 6,
	stVoid = FIRST_PASS_BY_REF + 7,
	stChunk = FIRST_PASS_BY_REF + 8
};

//==========================================================
// Currently only lpszScriptExt and lpszPrecompExt are used...
static LPCTSTR lpszScriptExt = _T(".d");		// ASCII basic files use this extension
static LPCTSTR lpszPrecompExt = _T(".dc");		// Precompiled files use this extension
static LPCTSTR lpszDLLExt = _T(".dl");			// DLL modules
static LPCTSTR lpszSerialExt = _T(".ds");		// Default serialized data extension

//=============================================================
// Change the following as needed...
#if defined(__WINNT__) || defined(__WINCE__)
#	define ERROR_MSG(title, message) MessageBox(NULL, (message), (title), MB_OK)
#	define GET_EXE_FILENAME(buf, bufSize) GetModuleFileName(NULL, buf, bufSize)
#	define INTERLOCKED_INC(x) InterlockedIncrement(&x)
#	define INTERLOCKED_DEC(x) InterlockedDecrement(&x)
#else
#	define ERROR_MSG(title, message) printf(_T("%s: %s\n"), (title), (message))
#	define GET_EXE_FILENAME(buf, bufSize) _tcsncpy(buf, "__fixme__", bufSize)
#	define INTERLOCKED_INC(x) (++(x))	// OK, not correct, find better
#	define INTERLOCKED_DEC(x) (--(x))	// OK, not correct, find better
#endif

//=============================================================
#endif // __DIALECT_GLOBALS__H___
